package com.dgshanger.sanhxiaofeisc.ui.nearby

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.CategoryItemEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import kotlinx.android.synthetic.main.item_person_center.view.*


/**
 * MerchantListFragment
 * 沉迷学习不能自拔
 * Describe：分类列表
 * Created by 雷小星🍀 on 2018/4/18 18:45.
 */
class CategoryListFragment : BaseMvpRecycleViewFragment<CategoryItemEntity>() {
    override val listLimit: Int
        get() = 10
    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun initEvent(view: View?) {
        super.initEvent(view)
        adapter?.setOnItemClickListener { position, entity ->
            startActivity(Intent(mContext, CategoryActivity::class.java)
                    .putExtra("category_id",entity.category_id)
                    .putExtra("category_name",entity.category_name))
        }
    }

    override fun createAdapter(): BaseRecycleAdapter<CategoryItemEntity, out BaseViewHolder<CategoryItemEntity>> = Adapter()

    override fun createModel(): BaseMvpRecycleViewContract.Model<CategoryItemEntity> = Model()

    class Model : BaseMvpRecycleViewContract.Model<CategoryItemEntity> {
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<CategoryItemEntity>>?) {
            AppApi.Api().getCategoryListData(page.toString())
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)
        }

    }

    private class Adapter : BaseRecycleAdapter<CategoryItemEntity, ViewHolder>() {
        override fun createViewHolder(view: View?, viewType: Int): ViewHolder = ViewHolder(view!!)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_person_center

    }

    private class ViewHolder(view: View) : BaseViewHolder<CategoryItemEntity>(view) {
        override fun bindView(position: Int, entity: CategoryItemEntity?) {
            ImageLoader.loadToUrl(itemView.context,itemView.iv_item_icon,Constants.getBaseUrl()+entity?.img)
            itemView.tv_item_title.text = entity?.category_name
        }

    }
}